﻿/*
 * Cratos Example
 * Cratos Example Program class
 * 
 * Copyright (c) 2011-2012 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Threading;
using System.Windows.Forms;
using System.Configuration;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

using Cratos.Desktop;
using Cratos.Tools.General;
using Cratos.Tools.DataObject;
using Cratos.Desktop.Persistence;

namespace Cratos.Example
{
	/// <summary>
	/// Example Program class
	/// </summary>
	public class Program
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		#endregion

		#region Class Variable declarations
		#endregion

		#region Class Constructors & Destructors
		#endregion

		#region Class Properties
		/// <summary>
		/// Application Unity Container
		/// </summary>
		public static IUnityContainer UnityContainer { get; set; }
		#endregion

		#region Class Methods
		/// <summary>
		/// Main Application Method
		/// </summary>
		static void Main()
		{
			// Declare the required variables
			CratosPersistence dbPersistence;
			CDualWcfService		dualWcfService;

			// Perform the relevant environment setup
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			try
			{
				// Create and Setup the Unity Container
				Program.UnityContainer = new UnityContainer().LoadConfiguration();

				// Set the relevant options
				CratosHelper.LogFilename = ConfigurationManager.AppSettings["LogFilename"];

				DOCratosGlobal.UnityContainer = Program.UnityContainer;

				DOCratosGlobal.PagingEnabled			      = (ConfigurationManager.AppSettings["CratosPagingEnabled"].ToUpper().Equals(bool.TrueString.ToUpper()));
				DOCratosGlobal.PagingMaxNoRecords	      = long.Parse(ConfigurationManager.AppSettings["CratosPagingMaxPageRecords"]);
				DOCratosGlobal.PagingPersistCollection  = (ConfigurationManager.AppSettings["CratosPagingPersist"] == bool.TrueString);
				DOCratosGlobal.PagingDBConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["CratosPagingDBConnection"]].ConnectionString;

				DOCratosGlobal.EntityAccessMethod                 = ConfigurationManager.AppSettings["CratosEntityAccess"];
				DOCratosGlobal.EntityCollectionAccessMethod       = ConfigurationManager.AppSettings["CratosEntityCollectionAccess"];
				DOCratosGlobal.EntityPagingCollectionAccessMethod = ConfigurationManager.AppSettings["CratosEntityPagingCollectionAccess"];

				DOCratosGlobal.DBConnectionName       = ConfigurationManager.AppSettings["DBConnectionName"];
				DOCratosGlobal.WcfServiceAddress			= new Uri(ConfigurationManager.AppSettings["CratosWcfServiceAddress"]);
				DOCratosGlobal.DualWcfServiceAddress	= new Uri(ConfigurationManager.AppSettings["CratosDualWcfServiceAddress"]);

				CWcfService.WcfServiceUri     = DOCratosGlobal.WcfServiceAddress;
				CDualWcfService.WcfServiceUri = DOCratosGlobal.DualWcfServiceAddress;

				// Create and Setup the Cratos Persistence DB
				if (DOCratosGlobal.PagingPersistCollection)
				{
					// Create and Setup the DB Persistence object
					dbPersistence                    = new CratosPersistence();
					dbPersistence.DBConnectionString = DOCratosGlobal.PagingDBConnectionString;
					dbPersistence.DBFilename         = ConfigurationManager.AppSettings["CratosPagingDBFilename"];

					// Create the required DB
					dbPersistence.DBCreateDatabase(true);
				}

				Thread.Sleep(1000);

				// Ping the two WCF Services
				if (!CWcfService.Ping())			{ Console.WriteLine("Unable to connect to Cratos WCF Service"); Console.ReadKey(); return; }

				dualWcfService = new CDualWcfService();
				if (!dualWcfService.Ping())	{ Console.WriteLine("Unable to connect to Cratos Dual WCF Service"); Console.ReadKey(); return; }
				dualWcfService = null;

				// Execute the application
				Application.Run(new MainForm());
			}
			catch (Exception runtimeException)
			{
				// Display the exception
				CratosHelper.LogMessage(CratosLogSeverity.Exception, "Cratos", "Unhandled exception occurred", runtimeException, true);
			}
			finally { }

			// Return to OS
			return;
		}
		#endregion

		#region Class Support Methods
		#endregion
	}
}
